home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 005 / region / region.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  6KB  |  219 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. /* region.c */
  31.  
  32. /* SIMPLE REGIONS EXAMPLE.... DRAW BEHIND A FENCE */
  33. /* Certain layers.library routines are used herein that aren't available
  34.  * until Amiga C compiler version 1.1 and beyond.  */
  35.  
  36. /* Author:  Rob Peck, 12/1/85
  37.  *
  38.  * This code may be freely utilized to create programs for the Amiga. 
  39.  */
  40.  
  41.  
  42. #include <exec/types.h>
  43. #include <graphics/gfx.h>
  44. #include <hardware/dmabits.h>
  45. #include <hardware/custom.h>
  46. #include <graphics/gfxmacros.h>
  47. #include <graphics/regions.h>
  48. #include <graphics/clip.h>
  49. #include <graphics/text.h>
  50. #include <hardware/blit.h>
  51. #include <graphics/gfxbase.h>
  52. #include <graphics/copper.h>
  53. #include <graphics/gels.h>
  54. #include <graphics/rastport.h>
  55. #include <graphics/view.h>
  56. #include <exec/exec.h>
  57. #include <graphics/layers.h>
  58.  
  59. #define FLAGS LAYERSIMPLE
  60. extern struct Layer *CreateUpfrontLayer();
  61.  
  62. struct GfxBase *GfxBase;
  63.  
  64. long LayersBase;
  65.  
  66. #define DEPTH 2  
  67. #define WIDTH 320 
  68. #define HEIGHT 200 
  69. #define NOT_ENOUGH_MEMORY -1000
  70. #define FOREVER for(;;) 
  71.  
  72. struct View *oldview;
  73. struct View v;
  74. struct ViewPort vp;
  75. struct ColorMap *cm;
  76. struct RasInfo ri;
  77. struct BitMap b;
  78. struct RastPort *rp;      /* one rastport for one layer */
  79.  
  80. short i,j,k,n;
  81. struct ColorMap *GetColorMap();
  82.  
  83. USHORT colortable[] = { 0x000, 0xf00, 0x0f0, 0x00f };
  84.          /* black, red, green, blue */
  85. UBYTE *displaymem;
  86. UWORD *colorpalette;
  87.  
  88. struct Layer_Info *li;
  89. struct Layer *layer;      /* one layer pointer */
  90.  
  91. extern struct Region *NewRegion();
  92. struct Region *rgn;      /* one region pointer */
  93. struct Rectangle rect[14];   /* some rectangle structures */
  94. struct Region *oldDamageList;
  95.  
  96. extern struct Layer_Info *NewLayerInfo();
  97.  
  98. main()
  99. {
  100.    SHORT x,y;
  101.  
  102.    GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  103.    if (GfxBase == NULL) exit(1);
  104.    LayersBase = OpenLibrary("layers.library",0); 
  105.    if(LayersBase == NULL) exit(2);
  106.  
  107.  
  108.    oldview = GfxBase->ActiView;
  109.  
  110.    li = NewLayerInfo();    /* v1.1 compiler only */
  111.    InitView(&v);
  112.    v.ViewPort = &vp;
  113.    InitVPort(&vp);
  114.    vp.DWidth = WIDTH;
  115.    vp.DHeight = HEIGHT;
  116.    vp.RasInfo = &ri;
  117.    InitBitMap(&b,DEPTH,WIDTH,HEIGHT);
  118.    ri.BitMap = &b;
  119.    ri.RxOffset = 0;   
  120.    ri.RyOffset = 0;
  121.    ri.Next = NULL;
  122.    cm = GetColorMap(4);   
  123.    colorpalette = (UWORD *)cm->ColorTable;
  124.    for(i=0; i<4; i++)
  125.       *colorpalette++ = colortable[i];
  126.    vp.ColorMap = cm;   
  127.    for(i=0; i<DEPTH; i++)
  128.    {
  129.            b.Planes[i] = (PLANEPTR)AllocRaster(WIDTH,HEIGHT);
  130.            if(b.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  131.    }
  132.  
  133.    MakeVPort( &v, &vp );   
  134.    MrgCop( &v );      
  135.    for(i=0; i<2; i++)
  136.       {
  137.       displaymem = (UBYTE *)b.Planes[i];
  138.       for(j=0; j<RASSIZE(WIDTH,HEIGHT); j++)
  139.          *displaymem++ = 0;   
  140.       /* zeros to all bytes of the display area */               }
  141.  
  142.    LoadView(&v);
  143.    layer = CreateUpfrontLayer(li,&b,0,0,200,140,FLAGS,NULL);
  144.    if(layer==NULL) exit(3);
  145.  
  146.    rp = layer->rp;
  147.  
  148.    SetAPen(rp,3);
  149.    RectFill(rp,0,0,199,139);   /* show the layer itself */
  150.  
  151.    j=10;         /* initialize the rectangles */ 
  152.    for(i=0; i<10; i++)
  153.       {
  154.       rect[i].MinX = j;
  155.       rect[i].MaxX = j + 8;
  156.       rect[i].MinY = 20;
  157.       rect[i].MaxY = 120;
  158.       j += 16;
  159.       }   
  160.       
  161.    rgn = NewRegion();   /* get a new region to use */
  162.    if(rgn == NULL) exit(4);
  163.  
  164.    for(i=0; i<14; i++)
  165.       OrRectRegion(rgn,&rect[i]);
  166.  
  167.    oldDamageList = layer->DamageList;
  168.    layer->DamageList = rgn;
  169.  
  170.    BeginUpdate(layer);
  171.  
  172.    /* here insert the drawing routines to draw something
  173.          * behind the slats.
  174.          */
  175.    x = 4;  y = 10;
  176.    SetAPen(rp,0);
  177.    SetDrMd(rp,JAM1);
  178.    RectFill(rp,0,0,199,139);
  179.    SetAPen(rp,1);
  180.    SetBPen(rp,0);
  181.    SetDrMd(rp,JAM2);
  182.    for(i=0; i<14; i++)
  183.    {
  184.       Move(rp, x, y);
  185.       Text(rp,"Behind A Fence",14);
  186.       x += 4;  y += 9;
  187.    }
  188.    EndUpdate(layer);
  189.    layer->DamageList = oldDamageList;
  190.    DisposeRegion(rgn);
  191.  
  192.    Delay(300);
  193.  
  194.    DeleteLayer(li, layer);
  195.    DisposeLayerInfo(li);
  196.  
  197.    LoadView(oldview);
  198.  
  199.    FreeMemory();   
  200.    CloseLibrary(GfxBase);
  201.  
  202. }    /* end of main() */
  203.  
  204.  
  205. FreeMemory()
  206. {            /* return user and system-allocated memory to sys manager */
  207.  
  208.      for(i=0; i<DEPTH; i++)         /* free the drawing area */
  209.            FreeRaster(b.Planes[i],WIDTH,HEIGHT);
  210.    FreeColorMap(cm);         /* free the color map */
  211.       /* free dynamically created structures */
  212.    FreeVPortCopLists(&vp);         
  213.    FreeCprList(v.LOFCprList);
  214.    return(0);
  215. }
  216.  
  217.  
  218.  
  219.